package com.sm.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sm.dao.TeacherMapper;
import com.sm.entity.*;
import com.sm.service.AdminService;
import com.sm.service.UserService;
import com.sm.utils.PageUtils;
import com.sm.utils.R;
import com.sm.utils.RD;
import com.sm.vo.CourseListVo;
import com.sm.vo.StudentVo;
import com.sm.vo.TeacherVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author liyixuan
 * @email liyixuan106@163.com
 * @date 2022-04-06 19:19:08
 */
@RestController
@SaCheckRole("admin")
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 查询管理员信息
     *
     * @param params
     * @return 分页查询学生类
     */
    @RequestMapping("/getAll")
    public RD getAllAdmin(@RequestParam Map<String, Object> params) {
        List<User> list = adminService.getAllAdmin(params);
        return RD.success().putData("list", list).putData("total", list.size());
    }

    /**
     * 添加管理员信息
     *
     * @param user
     * @return json
     */
    @RequestMapping("/add")
    public RD addAdmin(@RequestBody User user) {
        boolean result = adminService.addAdmin(user);
        if (result) {
            return RD.success();
        } else {
            return RD.fail();
        }
    }

    /**
     * 修改管理员信息
     *
     * @param id
     * @return json
     */
    @RequestMapping("/delete")
    public RD deleteAdmin(@RequestParam("id") Integer id) {
        boolean result = adminService.deleteAdmin(id);
        if (result) {
            return RD.success();
        } else {
            return RD.fail();
        }
    }

    /**
     * 更新管理员信息
     *
     * @param user
     * @return jsons
     */
    @RequestMapping("/update")
    public RD updateAdmin(@RequestBody User user) {
        boolean result = adminService.updateAdmin(user);
        if (result) {
            return RD.success();
        } else {
            return RD.fail();
        }
    }


    /**
     * 添加单个学生信息
     *
     * @param student 学生类
     * @return 成功消息
     */
    @RequestMapping("/student/add")
    public R addStudent(@RequestBody Student student) {
        Student studentAdd = new Student();
        BeanUtils.copyProperties(student, studentAdd);
        boolean result = adminService.addStudent(student);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    /**
     * 删除单个学生信息
     *
     * @param id 学生id
     * @return 成功消息
     */
    @RequestMapping("/student/delete")
    public R deleteStudent(@RequestParam("id") Integer id) {
        boolean result = adminService.deleteStudent(id);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    /**
     * // TODO: 2022/4/7 待测试 更新学生信息
     *
     * @param studentVo
     * @return 成功消息
     */
    @RequestMapping("/student/update")
    public R updateStudent(@RequestBody StudentVo studentVo) {
        boolean result = adminService.updateStudent(studentVo);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    /**
     * 查询学生信息
     *
     * @param params
     * @return 分页查询学生类
     */
    @RequestMapping("/student/getAll")
    public RD getAllStudent(@RequestParam Map<String, Object> params) {
        List<StudentVo> list = adminService.getAllStudentByPage(params);
        return RD.success().putData("list", list).putData("total", list.size());
    }

    /**
     * 添加教师信息
     *
     * @param teacher 教师类
     * @return 成功消息
     */
    @RequestMapping("/teacher/add")
    public R addTeacher(@RequestBody Teacher teacher) {
        boolean result = adminService.addTeacher(teacher);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    /**
     * 查询教师信息
     *
     * @param params
     * @return JSON
     */
    @RequestMapping("/teacher/getAll")
    public RD getAllTeachers(@RequestParam Map<String, Object> params) {
        List<TeacherVo> list = adminService.getAllTeachers(params);
        return RD.success().putData("list", list).putData("total", list.size());
    }

    /**
     * 删除教师信息
     *
     * @param id
     * @return 成功消息
     */
    @RequestMapping("/teacher/delete")
    public R deleteTeacher(@RequestParam("id") Integer id) {
        boolean result = adminService.deleteTeacher(id);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    /**
     * 更新教师信息
     *
     * @param teacherVo
     * @return 成功消息
     */
    @RequestMapping("/teacher/update")
    public R updateTeacher(@RequestBody TeacherVo teacherVo) {
        boolean result = adminService.updateTeacher(teacherVo);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    /**
     * 查询所有教师信息
     *
     * @return JSON
     */
    @RequestMapping("/teacher/getAll1")
    RD getAllTeachers() {
        List<Teacher> list = adminService.getAllTeachers();
        return RD.success().putData("list", list);
    }

    /**
     * 查询所有课程信息
     *
     * @return JSON
     */
    @RequestMapping("/courses/getAll")
    RD getAllCourse(@RequestParam Map<String, Object> params) {
        List<CourseListVo> list = adminService.getAllCourse(params);
        List<Teacher> teachers = teacherMapper.selectList(null);
        return RD.success().putData("list", list).putData("total", list.size()).putData("teachers", teachers);
    }


    /**
     * 添加课程信息
     *
     * @param courseList 课程类
     * @return 课程添加成功消息
     */
    @RequestMapping("/courses/add")
    public R addCourse(@RequestBody CourseListEntity courseList) {
        boolean result = adminService.addCourse(courseList);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    /**
     * 删除课程信息
     *
     * @return 成功消息
     */
    @RequestMapping("/courses/delete")
    public R deleteCourse(@RequestParam("id") Integer id) {
        boolean result = adminService.deleteCourse(id);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    /**
     * 修改课程信息
     *
     * @param courseList 课程类
     * @return 成功消息
     */
    @RequestMapping("/courses/update")
    public R updateCourse(@RequestBody CourseListEntity courseList) {
        boolean result = adminService.updateCourse(courseList);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    @RequestMapping("/home")
    public RD ViewPersonalInformation() {
        User user = null;
        try {
            user = userService.getUser();
        } catch (Exception e) {
            e.printStackTrace();
        }
        QueryWrapper<AdminEntity> wrapper = new QueryWrapper<>();
        if (user != null) {
            wrapper.eq("adm_username", user.getUsername());
            AdminEntity admin = adminService.getOne(wrapper);
            if (admin != null) {
                return RD.success().putData("data", admin);
            } else {
                return RD.fail();
            }
        }
        return RD.fail();

    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public RD list(@RequestParam Map<String, Object> params) {
        PageUtils page = adminService.queryPage(params);

        return RD.success().putData("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{admId}")
    public RD info(@PathVariable("admId") Integer admId) {
        AdminEntity admin = adminService.getById(admId);

        return RD.success().putData("admin", admin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AdminEntity admin) {
        adminService.save(admin);

        return R.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/v")
    public R update(@RequestBody AdminEntity admin) {
        adminService.updateById(admin);

        return R.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete1")
    public R delete(@RequestBody Integer[] admIds) {
        adminService.removeByIds(Arrays.asList(admIds));

        return R.success();
    }

}
